package com.iot.sdk.service.impl;

import com.dc3.common.constant.CommonConstant;
import com.dc3.common.constant.ValueConstant;
import com.dc3.common.model.DriverEvent;
import com.dc3.common.model.Point;
import com.iot.sdk.bean.driver.DriverContext;
import com.iot.sdk.service.DriverService;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @Author 咕唧
 * @Date 2022/7/26 19:22
 * @Version 1.0
 */
@Service
public class DriverServiceImpl implements DriverService {

    private DriverContext driverContext;

    @Value("${spring.application.name}")
    private String serviceName;

    private RabbitTemplate rabbitTemplate;

    @Autowired
    public void setRabbitTemplate(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @Autowired
    public void setDriverContext(DriverContext driverContext) {
        this.driverContext = driverContext;
    }

    @Override
    public String convertValue(String deviceId, String pointId, String rawString) {
        //根据设备id和位号id获取位号中定义的属性
        Point point = driverContext.getPointByDeviceIdAndPointId(deviceId, pointId);

        //根据定义好的类型执行预先定义的方法
        switch (point.getType()) {
            //如果期望的类型与返回类型一致，直接返回
            case ValueConstant.Type.STRING:
                return rawString;
            case ValueConstant.Type.INT:
            case ValueConstant.Type.BYTE:
            case ValueConstant.Type.SHORT:
            case ValueConstant.Type.DOUBLE:
            case ValueConstant.Type.LONG:
            case ValueConstant.Type.FLOAT:
                //都是期望数字类型,是数字类型的话我们就看看有没有约定最大值,最小值,或者是小数点后保留多少
                //当前值是不是小于小于最小值
                //转换为数字类型
                double temp = Double.parseDouble(rawString);
                if (point.getMinimum() != null && temp < point.getMinimum()) {
                    System.err.println("当前传递的数据小于最小值");
                }

                if (point.getMaximum() != null && temp > point.getMaximum()) {
                    System.err.println("当前传递的数据超过最大值");
                }

                if (StringUtils.hasText(point.getFormat())) {
                    return String.format(point.getFormat(), temp);
                } else {
                    return String.valueOf(temp);
                }
            case ValueConstant.Type.BOOLEAN:
                //一般来说只要大于0都是true,其他都是false
                double booleanValue = Double.parseDouble(rawString);//将传递的数据转成double
                if (booleanValue > 0) {
                    return Boolean.TRUE.toString();
                } else {
                    return Boolean.FALSE.toString();
                }
        }
        return null;
    }

    @Override
    public void driverEventSender(DriverEvent driverEvent) {
        //通过mq将数据发送到manager进行操作,等待数据的返回,但是实际上返回的只是注册成功相关的基本结果,没有其他的结果
        if (driverEvent != null) {
            rabbitTemplate.convertAndSend(CommonConstant.Rabbit.TOPIC_EXCHANGE_EVENT
                    , CommonConstant.Rabbit.ROUTING_DRIVER_EVENT_PREFIX + serviceName
                    , driverEvent);
        }
    }
}
